COVID-19 Data Analysis

BEST COUNTRIES OVERVIEW

Click on a country to remove

Double-click on a country to isolate

Data repository: link

Jupyter Notebook repository: link

In [1]:
import json
import requests
import datetime as dt

import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.offline as pyo

pyo.init_notebook_mode()
In [2]:
json_countries = "https://raw.githubusercontent.com/maxdevblock/covid-19-time-series/master/json/COVID-COUNTRIES.json"
with requests.get(json_countries) as req:
    data = json.loads(req.content.decode('utf-8-sig'))
    
# LOCKDOWNS
_ = """
ld = pd.read_csv(
    "https://raw.githubusercontent.com/maxdevblock/covid-19-time-series/master/csv/lockdowns.csv",
    dtype={"Place": str}
)
ld.fillna('', inplace=True)"""
In [3]:
print("FIRST ENTRY DATE: {}".format(
    list( data["US"]["Confirmed"].keys() )[0]
    )
)
print("LAST  ENTRY DATE: {}".format(
    list( data["US"]["Confirmed"].keys() )[-1]
    )
)
period = (
    dt.datetime.strptime(list(data["US"]["Confirmed"].keys())[-1], "%Y-%m-%d") -
    dt.datetime.strptime(list(data["US"]["Confirmed"].keys())[0], "%Y-%m-%d")
).days

print("COVERAGE: {} days".format(period))
print("CURRENT DATE IS: {}".format(dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
FIRST ENTRY DATE: 2020-01-22
LAST  ENTRY DATE: 2020-07-19
COVERAGE: 179 days
CURRENT DATE IS: 2020-07-20 09:06:08
In [4]:
def get_best_countries(json_data=None, number=25):
    LAST_CONFIRMED = {}
    for country in json_data:
        last_date = sorted(json_data[country]["Confirmed"])[-1]
        last_confirmed = json_data[country]["Confirmed"][last_date]
        LAST_CONFIRMED.update({
            country: last_confirmed
        })

    LAST_CONFIRMED = {k: v for k, v in sorted(LAST_CONFIRMED.items(), key=lambda item: item[1], reverse=True)}

    return sorted(list(LAST_CONFIRMED)[:number])
In [5]:
best_countries = get_best_countries(json_data=data)
print("COUNTRIES: {}".format(best_countries))
COUNTRIES: ['Argentina', 'Bangladesh', 'Brazil', 'Canada', 'Chile', 'Colombia', 'Egypt', 'France', 'Germany', 'India', 'Indonesia', 'Iran', 'Iraq', 'Italy', 'Mexico', 'Pakistan', 'Peru', 'Qatar', 'Russia', 'Saudi Arabia', 'South Africa', 'Spain', 'Turkey', 'US', 'United Kingdom']
In [6]:
x = []   # datetime x array
# _x = []  # integer x array
# yC = []  # new confirmed cases array
# yD = []  # new deaths array
# yR = []  # new recovered array
# yP = []  # new infected array

TOTyC = {}  # confirmed cases array
TOTyD = {}  # deaths array
TOTyR = {}  # recovered array
TOTyP = {}  # infected array
TOTyr = {}  # mortality rate
TOTyk = {}  # recovery rate
In [8]:
for day in data[best_countries[0]]["Confirmed"]:
    # x values
    x.append(dt.datetime.strptime(day, "%Y-%m-%d"))

for country in best_countries:
    TOTyC.update({country: np.array([])})
    TOTyD.update({country: np.array([])})
    TOTyR.update({country: np.array([])})
    TOTyP.update({country: np.array([])})
    TOTyr.update({country: np.array([])})
    TOTyk.update({country: np.array([])})
    for i, day in enumerate(data[country]["Confirmed"]):
        # y TOT values
        TOTyC[country] = np.append(TOTyC[country], data[country]["Confirmed"][day])
        TOTyD[country] = np.append(TOTyD[country], data[country]["Deaths"][day])
        TOTyR[country] = np.append(TOTyR[country], data[country]["Recovered"][day])
        TOTyP[country] = np.append(TOTyP[country], data[country]["Confirmed"][day] - data[country]["Deaths"][day] - data[country]["Recovered"][day])
        
        if data[country]["Confirmed"][day]:
            TOTyr[country] = np.append(TOTyr[country], data[country]["Deaths"][day] / data[country]["Confirmed"][day])
        else:
            TOTyr[country] = np.append(TOTyr[country], [.0])
            
        if data[country]["Confirmed"][day]:
            TOTyk[country] = np.append(TOTyk[country], data[country]["Recovered"][day] / data[country]["Confirmed"][day])
        else:
            TOTyk[country] = np.append(TOTyk[country], [.0])
            
minC = 20
minD = 20
minR = 20
minP = 20
In [9]:
# lockdowns

_ = """
now = dt.datetime.now()

fig = fig = go.Figure()

for i, _c in enumerate(ld["Country"]):
    start = dt.datetime.strptime(ld["Start date"][i], "%Y-%m-%d")
    if now < start:
        continue
    end = "undefined"
    if ld["End date"][i]:
        end = dt.datetime.strptime(ld["End date"][i], "%Y-%m-%d")
        if now > end:
            continue
    name = "{}{}".format(
        _c, 
        ", {}".format(ld["Place"][i]) if ld["Place"][i] else ""
    )
    fig.add_trace(
        go.Scattergeo(
            mode="markers", marker_size=25, opacity=.5,
            lat=[ld["lat"][i]],
            lon=[ld["lon"][i]],
            name=name,
            hovertext="<b>{}</b><br>{} start<br>{} end".format(
                name,
                start.strftime("%Y %m %d"),
                end if isinstance(end, str) else end.strftime("%Y %m %d")
            )
        )
    )
fig.update_layout(
    geo = dict(
        showcountries=True,
    ),
    title={"text": "Current known lockdowns ({})".format(now.strftime("%Y %m %d")), "xanchor": "center", "x": 0.5},
    showlegend=False
)
fig.show()"""

TOTAL CASES

In [10]:
fig = go.Figure()

for country in TOTyC:
    try:
        j = np.where(TOTyC[country] > minC)[0][0]
    except Exception as err:
        print("ERROR: {} max={} < min={}".format(country, max(TOTyC[country]), minC))
        continue
    X = [i for i in range(len(x[j:]))]
    Y = TOTyC[country][j:]
    fig.add_trace(go.Scatter(
        x=X, y=Y,
        mode='lines+markers',
        marker_size=3, marker_symbol="circle",
        line_shape='spline',
        name=country,
        hovertemplate="%{text}"+
        "<br>%{x}"+
        "<br>%{y:.0f}",
        text=[country for _ in range(len(x))]
    ))

fig.update_layout(legend_orientation="h",
    showlegend=True, plot_bgcolor='rgba(0,0,0,0)', 
    yaxis={"gridcolor": '#bdbdbd', "zerolinecolor": '#969696'},
    xaxis={"gridcolor": '#bdbdbd'},
    title={"text": "BEST 20 countries (cases since the day of the ~{:.0f}°)".format(minC), "xanchor": "center", "x": 0.5},
    yaxis_title="cases",
)

pyo.iplot(fig)
In [11]:
fig = go.Figure()

for country in TOTyC:
    fig.add_trace(go.Scatter(
        x=x, y=TOTyC[country],
        mode='lines+markers',
        marker_size=3, marker_symbol="circle",
        line_shape='spline',
        name=country,
        hovertemplate="%{text}"+
        "<br>%{x}"+
        "<br>%{y:.0f}",
        text=[country for _ in range(len(x))]
    ))

fig.update_layout(legend_orientation="h",
    showlegend=True, plot_bgcolor='rgba(0,0,0,0)', 
    yaxis={"gridcolor": '#bdbdbd', "zerolinecolor": '#969696'},
    xaxis={"gridcolor": '#bdbdbd'},
    title={"text": "BEST 20 countries (cases)", "xanchor": "center", "x": 0.5},
    yaxis_title="cases",
)

pyo.iplot(fig)
In [12]:
fig = go.Figure()

for country in TOTyC:
    fig.add_trace(go.Scatter(
        x=x, y=TOTyC[country],
        mode='lines+markers',
        marker_size=3, marker_symbol="circle",
        line_shape='spline',
        name=country,
        hovertemplate="%{text}"+
        "<br>%{x}"+
        "<br>%{y:.0f}",
        text=[country for _ in range(len(x))]
    ))

fig.update_layout(legend_orientation="h",
    showlegend=True, plot_bgcolor='rgba(0,0,0,0)', 
    yaxis={"gridcolor": '#bdbdbd', "zerolinecolor": '#969696'},
    xaxis={"gridcolor": '#bdbdbd'},
    title={"text": "BEST 20 countries (cases, log scale)", "xanchor": "center", "x": 0.5},
    yaxis_title="cases", yaxis_type="log",
)

pyo.iplot(fig)

DEATHS

In [13]:
fig = go.Figure()

for country in TOTyD:
    try:
        j = np.where(TOTyD[country] > minD)[0][0]
    except Exception as err:
        print("ERROR: {} max={} < min={}".format(country, max(TOTyD[country]), minD))
        continue
    X = [i for i in range(len(x[j:]))]
    Y = TOTyD[country][j:]
    fig.add_trace(go.Scatter(
        x=X, y=Y,
        mode='lines+markers',
        marker_size=3, marker_symbol="circle",
        line_shape='spline',
        name=country,
        hovertemplate="%{text}"+
        "<br>%{x}"+
        "<br>%{y:.0f}",
        text=[country for _ in range(len(x))]
    ))

fig.update_layout(legend_orientation="h",
    showlegend=True, plot_bgcolor='rgba(0,0,0,0)', 
    yaxis={"gridcolor": '#bdbdbd', "zerolinecolor": '#969696'},
    xaxis={"gridcolor": '#bdbdbd'},
    title={"text": "BEST 20 countries (deaths since the day of the ~{:.0f}°)".format(minD), "xanchor": "center", "x": 0.5},
    yaxis_title="deaths",
)

pyo.iplot(fig)
In [14]:
fig = go.Figure()

for country in TOTyD:
    fig.add_trace(go.Scatter(
        x=x, y=TOTyD[country],
        mode='lines+markers',
        marker_size=3, marker_symbol="circle",
        line_shape='spline',
        name=country,
        hovertemplate="%{text}"+
        "<br>%{x}"+
        "<br>%{y:.0f}",
        text=[country for _ in range(len(x))]
    ))

fig.update_layout(legend_orientation="h",
    showlegend=True, plot_bgcolor='rgba(0,0,0,0)', 
    yaxis={"gridcolor": '#bdbdbd', "zerolinecolor": '#969696'},
    xaxis={"gridcolor": '#bdbdbd'},
    title={"text": "BEST 20 countries (deaths)", "xanchor": "center", "x": 0.5},
    yaxis_title="deaths",
)

pyo.iplot(fig)
In [15]:
fig = go.Figure()

for country in TOTyD:
    fig.add_trace(go.Scatter(
        x=x, y=TOTyD[country],
        mode='lines+markers',
        marker_size=3, marker_symbol="circle",
        line_shape='spline',
        name=country,
        hovertemplate="%{text}"+
        "<br>%{x}"+
        "<br>%{y:.0f}",
        text=[country for _ in range(len(x))]
    ))

fig.update_layout(legend_orientation="h",
    showlegend=True, plot_bgcolor='rgba(0,0,0,0)', 
    yaxis={"gridcolor": '#bdbdbd', "zerolinecolor": '#969696'},
    xaxis={"gridcolor": '#bdbdbd'},
    title={"text": "BEST 20 countries (deaths, log scale)", "xanchor": "center", "x": 0.5},
    yaxis_title="deaths", yaxis_type="log",
)

pyo.iplot(fig)

RECOVERED

In [16]:
fig = go.Figure()

for country in TOTyR:
    try:
        j = np.where(TOTyR[country] > minR)[0][0]
    except Exception as err:
        print("ERROR: {} max={} < min={}".format(country, max(TOTyR[country]), minR))
        continue
    X = [i for i in range(len(x[j:]))]
    Y = TOTyR[country][j:]
    fig.add_trace(go.Scatter(
        x=X, y=Y,
        mode='lines+markers',
        marker_size=3, marker_symbol="circle",
        line_shape='spline',
        name=country,
        hovertemplate="%{text}"+
        "<br>%{x}"+
        "<br>%{y:.0f}",
        text=[country for _ in range(len(x))]
    ))

fig.update_layout(legend_orientation="h",
    showlegend=True, plot_bgcolor='rgba(0,0,0,0)', 
    yaxis={"gridcolor": '#bdbdbd', "zerolinecolor": '#969696'},
    xaxis={"gridcolor": '#bdbdbd'},
    title={"text": "BEST 20 countries (recovered since the day of the ~{:.0f}°)".format(minR), "xanchor": "center", "x": 0.5},
    yaxis_title="recovered",
)

pyo.iplot(fig)
In [17]:
fig = go.Figure()

for country in TOTyR:
    fig.add_trace(go.Scatter(
        x=x, y=TOTyR[country],
        mode='lines+markers',
        marker_size=3, marker_symbol="circle",
        line_shape='spline',
        name=country,
        hovertemplate="%{text}"+
        "<br>%{x}"+
        "<br>%{y:.0f}",
        text=[country for _ in range(len(x))]
    ))

fig.update_layout(legend_orientation="h",
    showlegend=True, plot_bgcolor='rgba(0,0,0,0)', 
    yaxis={"gridcolor": '#bdbdbd', "zerolinecolor": '#969696'},
    xaxis={"gridcolor": '#bdbdbd'},
    title={"text": "BEST 20 countries (recovered)", "xanchor": "center", "x": 0.5},
    yaxis_title="recovered",
)

pyo.iplot(fig)
In [18]:
fig = go.Figure()

for country in TOTyR:
    fig.add_trace(go.Scatter(
        x=x, y=TOTyR[country],
        mode='lines+markers',
        marker_size=3, marker_symbol="circle",
        line_shape='spline',
        name=country,
        hovertemplate="%{text}"+
        "<br>%{x}"+
        "<br>%{y:.0f}",
        text=[country for _ in range(len(x))]
    ))

fig.update_layout(legend_orientation="h",
    showlegend=True, plot_bgcolor='rgba(0,0,0,0)', 
    yaxis={"gridcolor": '#bdbdbd', "zerolinecolor": '#969696'},
    xaxis={"gridcolor": '#bdbdbd'},
    title={"text": "BEST 20 countries (recovered, log scale)", "xanchor": "center", "x": 0.5},
    yaxis_title="recovered",  yaxis_type="log",
)

pyo.iplot(fig)

INFECTED

In [19]:
fig = go.Figure()

for country in TOTyP:
    try:
        j = np.where(TOTyP[country] > minP)[0][0]
    except Exception as err:
        print("ERROR: {} max={} < min={}".format(country, max(TOTyP[country]), minP))
        continue
    X = [i for i in range(len(x[j:]))]
    Y = TOTyP[country][j:]
    fig.add_trace(go.Scatter(
        x=X, y=Y,
        mode='lines+markers',
        marker_size=3, marker_symbol="circle",
        line_shape='spline',
        name=country,
        hovertemplate="%{text}"+
        "<br>%{x}"+
        "<br>%{y:.0f}",
        text=[country for _ in range(len(x))]
    ))

fig.update_layout(legend_orientation="h",
    showlegend=True, plot_bgcolor='rgba(0,0,0,0)', 
    yaxis={"gridcolor": '#bdbdbd', "zerolinecolor": '#969696'},
    xaxis={"gridcolor": '#bdbdbd'},
    title={"text": "BEST 20 countries (infected since the day of the ~{:.0f}°)".format(minP), "xanchor": "center", "x": 0.5},
    yaxis_title="infected"
)

pyo.iplot(fig)
In [20]:
fig = go.Figure()

for country in TOTyP:
    fig.add_trace(go.Scatter(
        x=x, y=TOTyP[country],
        mode='lines+markers',
        marker_size=3, marker_symbol="circle",
        line_shape='spline',
        name=country,
        hovertemplate="%{text}"+
        "<br>%{x}"+
        "<br>%{y:.0f}",
        text=[country for _ in range(len(x))]
    ))

fig.update_layout(legend_orientation="h",
    showlegend=True, plot_bgcolor='rgba(0,0,0,0)', 
    yaxis={"gridcolor": '#bdbdbd', "zerolinecolor": '#969696'},
    xaxis={"gridcolor": '#bdbdbd'},
    title={"text": "BEST 20 countries (infected)", "xanchor": "center", "x": 0.5},
    yaxis_title="infected",
)

pyo.iplot(fig)
In [21]:
fig = go.Figure()

for country in TOTyP:
    fig.add_trace(go.Scatter(
        x=x, y=TOTyP[country],
        mode='lines+markers',
        marker_size=3, marker_symbol="circle",
        line_shape='spline',
        name=country,
        hovertemplate="%{text}"+
        "<br>%{x}"+
        "<br>%{y:.0f}",
        text=[country for _ in range(len(x))]
    ))

fig.update_layout(legend_orientation="h",
    showlegend=True, plot_bgcolor='rgba(0,0,0,0)', 
    yaxis={"gridcolor": '#bdbdbd', "zerolinecolor": '#969696'},
    xaxis={"gridcolor": '#bdbdbd'},
    title={"text": "BEST 20 countries (infected, log scale)", "xanchor": "center", "x": 0.5},
    yaxis_title="infected",  yaxis_type="log",
)

pyo.iplot(fig)

MORTALITY RATE

!!! PLEASE NOTE !!!

These rates are only useful for SIRD epidemiological model (read here for details) not to define COVID-19 actual rates.

In [22]:
fig = go.Figure()

for country in TOTyr:
    fig.add_trace(go.Scatter(
        x=x, y=TOTyr[country],
        mode='lines+markers',
        marker_size=3, marker_symbol="cross",
        line_shape='spline',
        name=country,
        hovertemplate="%{text}"+
        "<br>%{x}"+
        "<br>%{y:.2%}",
        text=[country for _ in range(len(x))]
    ))

fig.update_layout(legend_orientation="h",
    showlegend=True, plot_bgcolor='rgba(0,0,0,0)', 
    yaxis={"gridcolor": '#bdbdbd', "zerolinecolor": '#969696', "tickformat": ',.0%',},
    xaxis={"gridcolor": '#bdbdbd'},
    title={"text": "BEST 20 countries (mortality rate)", "xanchor": "center", "x": 0.5},
    yaxis_title="percentage",
)

pyo.iplot(fig)

RECOVERY RATE

In [23]:
fig = go.Figure()

for country in TOTyk:
    fig.add_trace(go.Scatter(
        x=x, y=TOTyk[country],
        mode='lines+markers',
        marker_size=3, marker_symbol="cross",
        line_shape='spline',
        name=country,
        hovertemplate="%{text}"+
        "<br>%{x}"+
        "<br>%{y:.2%}",
        text=[country for _ in range(len(x))]
    ))

fig.update_layout(legend_orientation="h",
    showlegend=True, plot_bgcolor='rgba(0,0,0,0)', 
    yaxis={"gridcolor": '#bdbdbd', "zerolinecolor": '#969696', "tickformat": ',.0%',},
    xaxis={"gridcolor": '#bdbdbd'},
    title={"text": "BEST 20 countries (recovery rate)", "xanchor": "center", "x": 0.5},
    yaxis_title="percentage",
)

pyo.iplot(fig)
In [ ]:
 

© 2020 Max Pierini & Sandra Mazzoli & Alessio Pamovio

Exported from countries/overview.ipynb committed by Max Pierini on Mon Aug 31 10:59:12 2020 revision 1, 8eb7ef8